package com.snj.visangedu.homestudy;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.google.gson.JsonArray;
import com.snj.visangedu.HomeActivity;
import com.snj.visangedu.R;
import com.snj.visangedu.lib.Base64;
import com.snj.visangedu.lib.CustomHttpClient;
import com.snj.visangedu.lib.Global;
import com.snj.visangedu.lib.PopupType;

public class HomeStudyFragment extends Fragment implements OnClickListener {
	private final String TAG = "HomeStudyFragment";
	
	String subject = "math";
	String content = "문제에 사용되는 개념을 모르겠어요.";
	
	
	
	int begin = 0;
	int cnt = 50;
	
	Button btn_ask = null;
	RelativeLayout rlly_ask = null;
	
	RelativeLayout rlly_takepic = null;
	ImageView img_takepic = null;
	
	RelativeLayout rlly_takemenu = null;
	ImageView img_takenpic = null;
	
	int takencount = 0; 
	LinearLayout lily_thumbnail = null;
	ImageView[] img_thumbnail = new ImageView[3];
	
	ImageView img_ok = null;
	ImageView img_add = null;
	ImageView img_takeagain = null;
	
	
	RelativeLayout rlly_asktype = null;
	RelativeLayout rlly_asktype0 = null;
	RelativeLayout rlly_asktype1 = null;
	RelativeLayout rlly_asktype2 = null;
	
	ImageView img_pencil = null;
	TextView txt_pencil = null;
	EditText edt_detailask = null;
	
	Button btn_math = null;
	Button btn_english = null;
	Button btn_science = null;
	Button btn_korean = null;
	Button btn_society = null;
	
	
	ImageView img_ask_back = null;
	ImageView img_ask_complete = null;
	
	TextView txt_asktype = null;
	TextView txt_asktype0 = null;
	TextView txt_asktype1 = null;
	TextView txt_asktype2 = null;
	
	
	ListView lstv_asks = null;
	AskAdapter _adapter = null;
	ArrayList<AskItem> arr_ask = new ArrayList<AskItem>();
	
	ArrayList<Bitmap> arr_bitmap = new ArrayList<Bitmap>();
			
	
	ScrollView scrv_homestudy = null;
	LinearLayout lily_homestudy = null;
	
	ImageView img_start = null;
	boolean isrunning = false;
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_homestudy,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		
		
		scrv_homestudy = (ScrollView)getView().findViewById(R.id.scrv_homestudy);
		lily_homestudy = (LinearLayout)getView().findViewById(R.id.lily_homestudy);
		
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("naverid", Global.naverid));
				
				Log.d(TAG, "param : " + postParameters.toString());
            	String result = "";
            	JSONObject resultobject = null;
            	
            	
            	try {
					result = CustomHttpClient.executeHttpPost(Global.getaskitemcnturl, postParameters);
					Log.d(TAG, "result : " + result);
					
					resultobject = new JSONObject(result);
					
					if(resultobject.getString("result").equals("success"))
					{
						return "success";
					}
					else
					{
						return null;
					}
            	}
            	catch(Exception e)
            	{
            		e.printStackTrace();
            		
            	}
            	return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				if(result != null)
				{					
					init_widget();
					scrv_homestudy.setVisibility(View.GONE);
				}
				else
				{					
					init_widget_notpaid();
					init_widget();
					rlly_ask.setVisibility(View.GONE);
				}
			}
			
			
			
		}.execute("");
		
		
	}
	
	private void init_widget_notpaid() {
		scrv_homestudy.setVisibility(View.VISIBLE);
		
		LinearLayout.LayoutParams param = null;
		
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int screenWidth = metrics.widthPixels;
		
		for(int i=0; i<4; i++)
		{
			ImageView edumax1 = new ImageView(getActivity());
		
			BitmapFactory.Options options = new BitmapFactory.Options();
			
			//options.inJustDecodeBounds = true;
			//BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);
			options.inSampleSize = 1;
			//options.inJustDecodeBounds = false;

			edumax1.setAdjustViewBounds(true);
			
			Bitmap img = null;

			try
			{
				img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("homestudy_" + i, "drawable", getActivity().getPackageName()), options);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			edumax1.setImageBitmap(img);
			
			edumax1.setScaleType(ScaleType.FIT_CENTER);
			
			switch(i)
			{
			case 0:
				param = new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
				param.setMargins(0, 62, 0, 0);
				edumax1.setLayoutParams(param);
			case 1:
				param = new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
				param.setMargins(0, 125, 0, 0);
				edumax1.setLayoutParams(param);
			case 2:
				param = new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
				param.setMargins(0, 135, 0, 0);
				edumax1.setLayoutParams(param);
				break;
			case 3:
				param = new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
				param.setMargins(0, 163, 0, 175);
				edumax1.setLayoutParams(param);
				break;
			}
			lily_homestudy.addView(edumax1);
		}
		
		
		ImageView edumax1 = new ImageView(getActivity());		
		edumax1.setPadding(0, 0, 0, 0);

		img_start = edumax1;
		edumax1.setAdjustViewBounds(true);
		edumax1.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.new_edumax_16));
		edumax1.setBackgroundResource(R.color.white_color);
		lily_homestudy.addView(edumax1);
		edumax1.setScaleType(ScaleType.FIT_CENTER);
		
		img_start.setOnClickListener(this);
		
		
		//scrv_edumax.getscr
		
		isrunning = true;
		new AsyncTask<String, String, String>() {
			
			

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				while(isrunning)
				{
					try {
						Thread.sleep(500);
						publishProgress("0");
						Thread.sleep(500);
						publishProgress("1");

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				return null;
			}

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				try
				{
					if(values[0].equals("0"))
					{
						img_start.setImageResource(R.drawable.new_edumax_16_1);
					}
					else
					{
						img_start.setImageResource(R.drawable.new_edumax_16);
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				Log.d(TAG, "PostExecute");
			}
			
			
			
		}.execute("");
	
	}
	
	private void init_widget() {
		
		
		
		View view = getView();
		
		btn_ask = (Button)view.findViewById(R.id.btn_ask);
		btn_ask.setOnClickListener(this);
		
		rlly_ask = (RelativeLayout)view.findViewById(R.id.rlly_ask);
		
		rlly_takepic = (RelativeLayout)view.findViewById(R.id.rlly_takepic);
		rlly_takepic.setOnClickListener(this);
		
		img_takepic = (ImageView)view.findViewById(R.id.img_takepic);
		img_takepic.setOnClickListener(this);
		
		rlly_takemenu = (RelativeLayout)view.findViewById(R.id.rlly_takemenu);
		img_takenpic = (ImageView)view.findViewById(R.id.img_takenpic);
		
		
		img_thumbnail[0] = (ImageView)view.findViewById(R.id.img_thumbnail0);
		img_thumbnail[1] = (ImageView)view.findViewById(R.id.img_thumbnail1);
		img_thumbnail[2] = (ImageView)view.findViewById(R.id.img_thumbnail2);
		
		
		lily_thumbnail = (LinearLayout)view.findViewById(R.id.lily_thumbnail);
		
		img_ok = (ImageView)view.findViewById(R.id.img_ok);
		img_ok.setOnClickListener(this);
		img_add = (ImageView)view.findViewById(R.id.img_add);
		img_add.setOnClickListener(this);
		img_takeagain = (ImageView)view.findViewById(R.id.img_takeagain);
		img_takeagain.setOnClickListener(this);
		
		rlly_asktype = (RelativeLayout)view.findViewById(R.id.rlly_asktype);
		
		rlly_asktype0 = (RelativeLayout)view.findViewById(R.id.rlly_asktype0);
		rlly_asktype0.setOnClickListener(this);
		rlly_asktype1 = (RelativeLayout)view.findViewById(R.id.rlly_asktype1);
		rlly_asktype1.setOnClickListener(this);
		rlly_asktype2 = (RelativeLayout)view.findViewById(R.id.rlly_asktype2);
		rlly_asktype2.setOnClickListener(this);
		
		img_pencil = (ImageView)view.findViewById(R.id.img_pencil);
		txt_pencil = (TextView)view.findViewById(R.id.txt_pencil);
		edt_detailask = (EditText)view.findViewById(R.id.edt_detailask);
		edt_detailask.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				img_pencil.setVisibility(View.GONE);
				txt_pencil.setVisibility(View.GONE);
				
				
				rlly_asktype0.setSelected(false);
				rlly_asktype1.setSelected(false);
				rlly_asktype2.setSelected(false);
						
				content = "";
				
				return false;
			}
		});
		
		
		btn_math = (Button)view.findViewById(R.id.btn_math);
		btn_math.setOnClickListener(this);
		btn_english = (Button)view.findViewById(R.id.btn_english);
		btn_english.setOnClickListener(this);
		btn_science = (Button)view.findViewById(R.id.btn_science);
		btn_science.setOnClickListener(this);
		btn_korean = (Button)view.findViewById(R.id.btn_korean);
		btn_korean.setOnClickListener(this);
		btn_society = (Button)view.findViewById(R.id.btn_society);
		btn_society.setOnClickListener(this);
		
		
		img_ask_back = (ImageView)view.findViewById(R.id.img_ask_back);
		img_ask_back.setOnClickListener(this);
		
		img_ask_complete = (ImageView)view.findViewById(R.id.img_ask_complete);
		img_ask_complete.setOnClickListener(this);
		
		
		Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/NANUMBARUNGOTHICLIGHT.TTF");
		btn_math.setTypeface(face);
		btn_english.setTypeface(face);
		btn_science.setTypeface(face);
		btn_korean.setTypeface(face);
		btn_society.setTypeface(face);
		
		((TextView)view.findViewById(R.id.txt_asktype)).setTypeface(face);
		((TextView)view.findViewById(R.id.txt_question0)).setTypeface(face);
		((TextView)view.findViewById(R.id.txt_question1)).setTypeface(face);
		((TextView)view.findViewById(R.id.txt_question2)).setTypeface(face);
		edt_detailask.setTypeface(face);
		
		
		
		btn_math.setSelected(true);
		rlly_asktype0.setSelected(true);
		
		
		
		lstv_asks = (ListView)view.findViewById(R.id.lstv_asks);
		
		begin = 0;
		arr_ask.clear();
		_adapter = new AskAdapter(getActivity(), R.layout.list_ask, arr_ask);
		
		lstv_asks.setAdapter(_adapter);
		
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				edt_detailask.setFocusable(true);
				edt_detailask.setFocusableInTouchMode(true);
			}
		}, 1000);
		
		
	}
	
    
    
	
	@Override
	public void onResume() {
		
		GetAsk(cnt);
		
		super.onResume();
	}
	
	PictureCallback mPicture = new PictureCallback() {
 		public void onPictureTaken(byte[] data, Camera camera) {
 			
 			/*
 			Calendar calendar = Calendar.getInstance();
 			String FileName = String.format("SH%02d%02d%02d-%02d%02d%02d.jpg", 
 					calendar.get(Calendar.YEAR) % 100, calendar.get(Calendar.MONTH)+1, 
 					calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), 
 					calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
 			String path = Global.ex_storage + "/visangedu/" + FileName;

 			File file = new File(path);
 			try {
 				FileOutputStream fos = new FileOutputStream(file);
 				fos.write(data);
 				fos.flush();
 				fos.close();
 			} catch (Exception e) {
 				
 				return;
 			}
 			
 			Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 			Uri uri = Uri.parse("file://" + path);
 			intent.setData(uri);
 			getActivity().sendBroadcast(intent);

 			Toast.makeText(getActivity(), "�궗吏꾩씠 ���옣 �릺�뿀�뒿�땲�떎", 0).show();*/
 			
 			
 			camera.startPreview();
 			
 			
 			rlly_takepic.setVisibility(View.GONE);
 			rlly_takemenu.setVisibility(View.VISIBLE);
 			
 			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
 			
 			
 			Matrix matrix = new Matrix();
 			matrix.postRotate(90);
 			
 			Bitmap rotatedbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
 			bm.recycle(); 
 			
 			img_takenpic.setImageBitmap(rotatedbm);
 			arr_bitmap.add(0, rotatedbm);
 			
 			for(int i=1; i<arr_bitmap.size(); i++)
 			{
 				img_thumbnail[i-1].setImageBitmap(arr_bitmap.get(i));
 			}
 			 
 			if(arr_bitmap.size() > 1)
 			{
 				lily_thumbnail.setVisibility(View.VISIBLE);
 				
 				for(int i=0; i<3; i++)
 				{
 					if(i < (arr_bitmap.size() - 1))
 					{
 						Log.d(TAG, "img_thumbnail[" + i + "] is visible");
 						img_thumbnail[i].setVisibility(View.VISIBLE); 						
 					}
 					else
 					{
 						img_thumbnail[i].setVisibility(View.INVISIBLE); 
 					}
 				}
 			}
 			else
 			{
 				lily_thumbnail.setVisibility(View.GONE);
 			}
 			
 		}
 	};

	@Override
	public void onClick(View v) {
		
		Log.d(TAG, "clicked");
		switch(v.getId())
		{
		
		case R.id.btn_ask: 
			Log.d(TAG, "btn_ask is clicked");
			rlly_ask.setVisibility(View.GONE);
			rlly_takepic.setVisibility(View.VISIBLE);
			break;
		case R.id.img_takepic:
			//((HomeActivity)getActivity()).mSurface.mCamera.autoFocus(null);
			((HomeActivity)getActivity()).mSurface.mCamera.takePicture(null, null, mPicture);
			break;
		case R.id.rlly_takepic:
			((HomeActivity)getActivity()).mSurface.mCamera.autoFocus(null);
			break;
		case R.id.img_ok:
			rlly_takemenu.setVisibility(View.GONE);
			rlly_asktype.setVisibility(View.VISIBLE);
			break;
		case R.id.img_add:
			if(arr_bitmap.size() < 3)
			{
				rlly_takemenu.setVisibility(View.GONE);
				rlly_takepic.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.img_takeagain:
			
			img_thumbnail[0].setImageBitmap(null);
			img_thumbnail[1].setImageBitmap(null);
			img_thumbnail[2].setImageBitmap(null);
			
			img_takenpic.setImageBitmap(null);
			
			arr_bitmap.clear();
			
			rlly_takemenu.setVisibility(View.GONE);
			rlly_takepic.setVisibility(View.VISIBLE);
			break;
		case R.id.rlly_asktype0:
			rlly_asktype0.setSelected(true);
			rlly_asktype1.setSelected(false);
			rlly_asktype2.setSelected(false);
			
			content = "문제에 사용되는 개념을 모르겠어요.";
			break;
		case R.id.rlly_asktype1:
			rlly_asktype0.setSelected(false);
			rlly_asktype1.setSelected(true);
			rlly_asktype2.setSelected(false);
			
			content = "개념 적용을 못하겠어요.";
			break;
		case R.id.rlly_asktype2:
			rlly_asktype0.setSelected(false);
			rlly_asktype1.setSelected(false);
			rlly_asktype2.setSelected(true);
			
			content = "풀이법을 모르겠어요.";
			break;
			
		case R.id.btn_math:
			btn_math.setSelected(true);
			btn_english.setSelected(false);
			btn_science.setSelected(false);
			btn_korean.setSelected(false);
			btn_society.setSelected(false);
			
			subject = "수학";
			break;
		case R.id.btn_english:
			btn_math.setSelected(false);
			btn_english.setSelected(true);
			btn_science.setSelected(false);
			btn_korean.setSelected(false);
			btn_society.setSelected(false);
			
			subject = "영어";
			break;
		case R.id.btn_science:
			btn_math.setSelected(false);
			btn_english.setSelected(false);
			btn_science.setSelected(true);
			btn_korean.setSelected(false);
			btn_society.setSelected(false);
			
			subject = "과학";
			break;
		case R.id.btn_korean:
			btn_math.setSelected(false);
			btn_english.setSelected(false);
			btn_science.setSelected(false);
			btn_korean.setSelected(true);
			btn_society.setSelected(false);
			
			subject = "국어";
			break;
		case R.id.btn_society:
			btn_math.setSelected(false);
			btn_english.setSelected(false);
			btn_science.setSelected(false);
			btn_korean.setSelected(false);
			btn_society.setSelected(true);
			
			subject = "사회";
			break;
			
		case R.id.img_ask_back:
			rlly_asktype.setVisibility(View.GONE);
			rlly_ask.setVisibility(View.VISIBLE);
			rlly_takepic.setVisibility(View.GONE);
			
			arr_bitmap.clear();
			break;
		case R.id.img_ask_complete:			
			UploadAsk();		
			break;
		default: 
			if(v.equals(img_start))
			{
				rlly_ask.setVisibility(View.VISIBLE);
				scrv_homestudy.setVisibility(View.GONE);
				
			}
			break;
		}
		
		
	}
	
	void UploadAsk()
	{
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("student", Global.naverid));
				postParameters.add(new BasicNameValuePair("subject", subject));
				postParameters.add(new BasicNameValuePair("content", (content.equals("") ? edt_detailask.getText().toString() : content)));
				
				Log.d(TAG, "param : " + postParameters.toString());
            	String result = "";
            	JSONObject resultobject = null;
            	
            	
            	try {
					result = CustomHttpClient.executeHttpPostImage(Global.addaskurl, postParameters);
					
					Log.d(TAG, "result : " + result);
					
					resultobject = new JSONObject(result);
					
					if(!resultobject.getString("result").equals("success")) return null;
					
					postParameters.clear();
					
					postParameters.add(new BasicNameValuePair("index", resultobject.getString("id")));
					postParameters.add(new BasicNameValuePair("student", Global.naverid));
					
					for(int i=0; i<arr_bitmap.size(); i++)
	            	{
	            		ByteArrayOutputStream bao = new ByteArrayOutputStream();
						
						int max = 1024;
						
						double rate = 0;
						int newHeight = 0;
						int newWidth = 0;
						
						if(arr_bitmap.get(i).getWidth() > max)
						{
							rate = (double)(max) / (double)arr_bitmap.get(i).getWidth();
							
							newWidth = max;
							newHeight = (int)((double)arr_bitmap.get(i).getHeight() * rate);
						}
						else if(arr_bitmap.get(i).getHeight() > max)
						{
							rate = (double)(max) / (double)arr_bitmap.get(i)	.getHeight();
							
							newWidth = (int)((double)arr_bitmap.get(i).getWidth() * rate);
							newHeight = max;
						}
						
						Log.d(TAG, "new size : " + newWidth + ", " + newHeight + "(" + rate + ")");
						
						Bitmap.createScaledBitmap(arr_bitmap.get(i), newWidth, newHeight, true).compress(Bitmap.CompressFormat.JPEG, 100, bao);
	            		
	    				byte[] ba = bao.toByteArray();
	    				String ba1 = Base64.encodeBytes(ba);	    				
	    				postParameters.add(new BasicNameValuePair("askimg" + i, ba1));
	            	}
					
					result = CustomHttpClient.executeHttpPostImage(Global.addaskimageurl, postParameters);
					
					Log.d(TAG, "result : " + result);
					
					resultobject = new JSONObject(result);
					
					if(resultobject.getString("result").equals("success")) return "success";
					/*
					JSONArray arr = resultobject.getJSONArray("data");
					*/					 
            	}
            	catch(Exception e)
            	{
            		e.printStackTrace();
            	}
            	
				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				if(result != null && result.equals("success"))
				{
					((HomeActivity)getActivity()).Show_Popup(PopupType.AskSent);
					
					rlly_asktype.setVisibility(View.GONE);
					rlly_ask.setVisibility(View.VISIBLE);
					rlly_takepic.setVisibility(View.GONE);
					GetLastAsk(1);
					arr_bitmap.clear();	
				}
			}
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
		Log.d(TAG, "Upload ask");
	}
	
	void GetAsk(int _cnt)
	{
		new AsyncTask<String, String, String>() {

			Bitmap askimg = null;
			int no = 0, itemno = 0;
			
			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("student", Global.naverid));
				postParameters.add(new BasicNameValuePair("begin", String.format("%d", 0)));
				postParameters.add(new BasicNameValuePair("cnt", params[0]));
				
				Log.d(TAG, "param : " + postParameters.toString());
            	String result = "";
            	JSONObject resultobject = null;
            	
            	
            	try {
					result = CustomHttpClient.executeHttpPost(Global.getaskurl, postParameters);
					
					if(result.equals("")) return  null;
					Log.d(TAG, "result : " + result);
					resultobject = new JSONObject(result);
					
					JSONArray resultarray = resultobject.getJSONArray("data");
					for(int i=0; i<resultarray.length(); i++)
					{
						JSONObject itemobject = resultarray.getJSONObject(i);
						
						ArrayList<Bitmap> arr_askimgs = new ArrayList<Bitmap>();
						
						
						for(int j=0; j<3; j++)
						{
							if(!itemobject.getString("ask_pic" + j).equals(""))
							{
								askimg = CustomHttpClient.downloadBitmap(Global.godourl + "homestudy/" + itemobject.getString("ask_pic" + j));
								arr_askimgs.add(askimg);
							}
						}
						
						
						arr_ask.add(new AskItem(itemobject.getString("no"), itemobject.getString("student"), "", "", itemobject.getString("content"), "", "", arr_askimgs, null));
						
						if(i != 0 && (i % 3) == 0)
						{
							publishProgress("");
						}
					}
					
					/*
					askimg = CustomHttpClient.downloadBitmap(Global.godourl + "homestudy/" + resultobject.getString("ask_pic0"));
					publishProgress(resultobject.getString("content"));
					//resultobject = new JSONObject(result);*/
					
            	}
            	catch(Exception e)
            	{
            		e.printStackTrace();
            	}
				return "";
			}
			
			

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				_adapter.notifyDataSetChanged();
			}



			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				
				_adapter.notifyDataSetChanged();
				
			}
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, String.format("%d", cnt));
	}
	
	void GetLastAsk(int _cnt)
	{
		new AsyncTask<String, String, String>() {

			Bitmap askimg = null;
			
			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("student", Global.naverid));
				postParameters.add(new BasicNameValuePair("begin", String.format("%d", 0)));
				postParameters.add(new BasicNameValuePair("cnt", params[0]));
				
				Log.d(TAG, "param : " + postParameters.toString());
            	String result = "";
            	JSONObject resultobject = null;
            	
            	
            	try {
					result = CustomHttpClient.executeHttpPost(Global.getlastaskurl, postParameters);
					
					if(result.equals("")) return  null;
					Log.d(TAG, "result : " + result);
					resultobject = new JSONObject(result);
					ArrayList<Bitmap> arr_askimgs = new ArrayList<Bitmap>();
					
					for(int j=0; j<3; j++)
					{
						if(!resultobject.getString("ask_pic" + j).equals(""))
						{
							askimg = CustomHttpClient.downloadBitmap(Global.godourl + "homestudy/" + resultobject.getString("ask_pic" + j));
							arr_askimgs.add(askimg);
						}
					}
					
					arr_ask.add(0, new AskItem(resultobject.getString("no"), resultobject.getString("student"), "", "", resultobject.getString("content"), "", "", arr_askimgs, null));
					
					/*
					askimg = CustomHttpClient.downloadBitmap(Global.godourl + "homestudy/" + resultobject.getString("ask_pic0"));
					publishProgress(resultobject.getString("content"));
					//resultobject = new JSONObject(result);*/
					
            	}
            	catch(Exception e)
            	{
            		e.printStackTrace();
            	}
				return "";
			}
			
			

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				_adapter.notifyDataSetChanged();
			}



			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				
				_adapter.notifyDataSetChanged();
				
			}
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, String.format("%d", cnt));
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
		
		super.onPause();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		((HomeActivity)getActivity()).StopMedia();
		
		super.onDestroy();
		
		
	}
	
	
}
